package com.mach.platform.service;

import com.mach.platform.domain.iot.IotConfigDevice;
import com.mach.platform.repository.ConfigDeviceRepo;
import com.mach.platform.utils.UUIDUtils;
import com.mach.platform.utils.springdata.DynamicSpecifications;
import com.mach.platform.utils.springdata.SearchFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @Description:
 * @Package: com.mach.platform.service
 * @time 2018/9/27 0027 13:03
 */
@Service
public class ConfigDeviceServiceImpl implements BaseService<IotConfigDevice>{

	@Autowired
	private ConfigDeviceRepo configDeviceRepo;
	/**
	 * 保存
	 *
	 * @param iotConfigDevice 保存的实体
	 * @return T 返回保存后的实体
	 */
    @CacheEvict(cacheNames = "devConfig", allEntries = true)
    @Transactional(rollbackOn = Exception.class)
	@Override
	public IotConfigDevice save (IotConfigDevice iotConfigDevice) {
		return configDeviceRepo.saveAndFlush(iotConfigDevice);
	}

	/**
	 * 插入，批量保存
	 *
	 * @param list 实体集合
	 * @return List<T>
	 */
	@Override
	public List<IotConfigDevice> insertObjInBatch (List<IotConfigDevice> list) {
		return null;
	}

	/**
	 * 更新
	 *
	 * @param iotConfigDevice 跟新的实体
	 * @return T
	 */
	@CacheEvict(value = "devConfig", key = "#iotConfigDevice.uuid")
	@Transactional(rollbackOn = Exception.class)
	@Override
	public IotConfigDevice update (IotConfigDevice iotConfigDevice) {
		return configDeviceRepo.saveAndFlush(iotConfigDevice);
	}

	/**
	 * 删除--批量删除
	 *
	 * @param ids 批量删除id
	 * @return int 影响数
	 */
    @CacheEvict(cacheNames = "devConfig", allEntries = true)
    @Transactional(rollbackOn = Exception.class)
	@Override
	public int delete (Long[] ids) {
		return configDeviceRepo.deleteByIdIn(ids);
	}

	/**
	 * 获取列表数据---使用 com.mach.platform.utils.springdata  下的DynamicSpecifications 的
	 * 写法进行分页查询
	 *
	 * @param pageable 分页参数
	 * @param params   其他参数
	 * @return Page<T>
	 */
	@Override
	public Page<IotConfigDevice> list (Pageable pageable, Map params) {
		Specification specification = DynamicSpecifications
				                              .bySearchFilter(SearchFilter.parse(params).values(), IotConfigDevice.class);

		return configDeviceRepo.findAll(specification, pageable);
	}

	/**
	 * 获取单体 数据
	 *
	 * @param id 获取详情的id
	 * @return T
	 */
	@Override
	public IotConfigDevice info (Long id) {
		return null;
	}

	/**
	 * 批量保存
	 * @param datas
	 * @return
	 */
    @CacheEvict(cacheNames = "devConfig", allEntries = true)
    @Transactional(rollbackOn = Exception.class)
	public int saveList(List<IotConfigDevice> datas) {

		int resultFlag = 0;
		if (datas.size() > 0) {
			if (datas.get(0).getUuid() == null){
				String uuid = UUIDUtils.generateUUID();
				datas = datas.stream().map(item -> {
					item.setUuid(uuid);
					return item;
				}).collect(Collectors.toList());
			}

			List<IotConfigDevice> iotConfigDevices = configDeviceRepo.saveAll(datas);
			resultFlag = iotConfigDevices.size();
		}

		return resultFlag;

	}

}
